<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getClassDes, getClassVideo, getAllseries, getClassList } from '../api/class/classdes'
import { useStore } from '../stores/class'
import { showToast, Toast } from 'vant'

export default defineComponent({
  components: {},
  setup() {
    let router: any = useRouter()
    let route: any = useRoute()
    const store = useStore()
    let groupId = route.query.groupId
    let paymentflag = route.query.payment
    let active = ref(0)
    let classDes: any = ref(null)
    let intro: any = ref(null)
    let newintro: any = ref(null)
    let seriesid: any = ref(null)
    let freeSeries: any = ref({})
    let fiveSeries: any = ref(null)
    let moreflag = ref(false)
    let videoDes: any = ref(null)
    let price = route.query.price
    let payflag = ref(false)

    let videourl = ref<string>()
    let buyClass: any = (): any => {
      router.push({
        path: '/classorder',
        query: {
          groupId,
          seriesid: seriesid.value
        }
      })
    }
    let back = () => {
      router.push({
        path: '/classDes',
        query: {
          groupId,
          price: price
        }
      })
    }
    let getClassDesFun = (groupId: any) => {
      getClassDes(groupId).then((data: any) => {
        console.log('课程详情数据=>', data)
        classDes.value = data.data
        console.log('课程详情数据classDes=>', classDes.value)
        intro.value = classDes.value.intro
        // console.log("intro=>", intro.value);
        newintro.value = intro.value.replace(/width="*\d+/g, 'width=375')
        newintro.value = newintro.value.replace(/height="*\d+/g, 'height=auto')
        newintro.value = newintro.value.replace(/<p+/g, "<p style='padding:0 10px;font-size:14px'")
        newintro.value = newintro.value.replace(
          /<strong+/g,
          "<strong style='padding:0 10px;font-size:17px;'"
        )
        newintro.value = newintro.value.replace(
          /<h2+/g,
          "<h2 style='padding:0 20px;font-size:16px;'"
        )
        // console.log("newintro=>", newintro.value);
      })
    }
    //课程数据
    let getClassVideoFun = (videoid: any) => {
      getClassVideo(videoid).then((data: any) => {
        console.log('视频课程数据=>', data)
        videourl.value = data.data.videoUrl
        videoDes.value = data.data
        console.log('videoDes=>', videoDes)
        console.log('videourl.value=>', videourl.value)
      })
    }
    //获得seriesid
    let getAllseriesFun = (groupId: any) => {
      getAllseries(groupId).then((data: any) => {
        console.log('课程seriesid数据=>', data)
        seriesid.value = data.data.items[0].seriesId
        console.log('seriesid=>', seriesid.value)
        getClassListFun(seriesid.value, 10)
      })
    }
    //获得课程数据
    let getClassListFun = (seriesid: number, limit: any) => {
      getClassList(seriesid, limit).then((data: any) => {
        console.log('所有章节数据=>', data)
        freeSeries.value = data.data
        console.log('所有章节数据freeSeries=>', freeSeries.value)
        // fiveSeries.value = freeSeries.value.items.filter((item: any, index: number) => {
        //   return index < 5;
        // })
      })
    }
    let more = () => {
      getClassListFun(seriesid.value, '')
      moreflag.value = !moreflag.value
    }
    let click_video = (vid: number, index: number) => {
      console.log('index=>', index)

      if (index < 2) {
        //点击了前两个课程
        router.push({
          path: '/classvideo',
          query: {
            groupId,
            vid,
            seriesid: seriesid.value,
            price: price
          }
        })
        console.log('点击了章节', vid)
        getClassVideoFun(vid)
      } else {
        //点击了后两个课程
        if (payflag.value == true) {
          //判断是否报名，若已报名
          router.push({
            path: '/classvideo',
            query: {
              groupId,
              vid,
              price: price,
              seriesid: seriesid.value
            }
          })
          console.log('点击了章节', vid)
          getClassVideoFun(vid)
        } else {
          //没有报名
          showToast('请返回报名后观看')
        }
      }
    }
    let changevideo = (vid: number) => {
      getClassVideoFun(vid)
    }
    onMounted(() => {
      getClassDesFun(route.query.groupId)
      getClassVideoFun(route.query.vid)
      getAllseriesFun(route.query.groupId)
      paymentflag = route.query.payment
      store.payClass.forEach((item: any) => {
        if (Number(item.groupId) == Number(route.query.groupId)) {
          if (item.payment == 1) {
            payflag.value = true
            return
          }
        }
      })
    })
    // https://www.cctalk.com/webapi/im/v1.3/group/86370728/info?isRichInfo=true&_timestamp=1686121670762
    return {
      active,
      buyClass,
      back,
      classDes,
      newintro,
      freeSeries,
      fiveSeries,
      more,
      moreflag, //是否显示更多按钮
      paymentflag, //是否支付完毕
      click_video, //点击章节方法
      videourl,
      videoDes,
      changevideo,
      payflag,
      price
    }
  }
})
</script>
<template>
  <div class="class-all-des" v-if="videoDes">
    <div class="top-fixed">
      <div class="desbar">
        <span class="iconfont icon-xiangzuojiantou" @click="back"> </span>
        <h2>课程详情</h2>
      </div>
      <!-- 课程视频 -->
      <div class="class-video">
        <!-- <img :src="classDes.avatar" alt="" /> -->
        <video :src="videourl" controls autoplay="true"></video>
      </div>
      <!-- 课程的详细信息 -->
      <div class="descript">
        <!-- 课程名字 -->
        <div class="class-name">{{ videoDes.videoName }}</div>
      </div>
    </div>

    <!-- 课程简介与免费试学 -->
    <div class="intro">
      <van-tabs v-model:active="active" animated sticky :offset-top="315">
        <van-tab title="简介" title-class="tab1">
          <div class="info" v-html="newintro" v-if="newintro"></div>
          <div class="info" v-else style="text-align: center;font-size: 14px;
          color:rgb(153, 146, 146)">暂无简介</div>
        </van-tab>
        <van-tab title="目录" title-class="tab1">
          <div class="title">
            课程
            <span>({{ classDes.videoCount }})</span>
          </div>
          <!-- 免费试学 -->
          <div class="freeclass" v-if="freeSeries.items">
            <div class="free-class-video">
              <div class="image">
                <img :src="classDes.avatar" alt="" />
              </div>
              <div class="word">
                {{ videoDes.videoName }}
              </div>
            </div>
            <!-- 试学列表 -->
            <div class="free-class-list">
              <ul>
                <li v-for="(item, index) in freeSeries.items" @click="click_video(item.videoInfo.videoId, index)">
                  <div class="smalltag" v-if=" index < 2 && !payflag">试学</div>
                  <div class="nulltag" v-else-if=" index >= 2 && !payflag"></div>
                  <div class="free-right">
                    <div class="free-class-name one-text-cut">{{ item.videoInfo.videoName }}</div>
                    <div class="free-count">{{ item.videoInfo.playCount }}次播放</div>
                  </div>
                </li>
              </ul>
            </div>
            <div class="more" @click="more" v-if="!moreflag && freeSeries.items.length >= 10">
              查看更多课程
            </div>
            <div class="bottom"></div>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.class-all-des {
  background-color: #f6f7f9;
  height: 100%;
}
.top-fixed {
  position: sticky;
  top: 0;
  z-index: 1000;
}
.desbar {
  display: flex;
  line-height: 44px;
  padding: 0 10px;
  position: sticky;
  z-index: 1000;
  top: 0;
  background-color: #f6f7f9;
  span {
    font-size: 20px;
    font-weight: 600;
  }
  h2 {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;

    // margin-left: 50px;
  }
}
.class-video {
  width: 100%;
  // height: 212px;
  position: relative;
  video {
    width: 100%;
    height: auto;
    display: block;
  }
  img {
    width: 100%;
    height: 212px;
  }
}
.descript {
  padding: 10px;
  height: 41px;
  background-color: #f6f7f9;
  margin-top: -2px;
}
.class-name {
  line-height: 40px;
  font-size: 20px;
  font-weight: 600;
}
::v-deep .van-tab__panel {
  height: 60vh;
  overflow-y: scroll;
}
.intro {
  background-color: #f6f7f9;

  .info {
    margin-top: 10px;
    img {
      width: 375px !important;
      height: auto !important;
    }
  }
  .tab1 {
    background-color: #f6f7f9;
  }
  ::v-deep .van-tab {
    background-color: #f6f7f9;
  }
  .title {
    margin: 0 16px;
    line-height: 38px;
    font-size: 20px;
    font-weight: 600;
    margin-top: 10px;
    span {
      color: #687583;
      font-size: 14px;
      margin-left: 8px;
      margin-top: -3px;
    }
  }
  .freeclass {
    margin: 0 8px;
    border-radius: 10px;
    overflow: hidden;
    .free-class-video {
      display: flex;
      background-color: #fff;
      padding: 10px;

      .image {
        img {
          width: 110px;
          height: auto;
          border-radius: 10px;
        }
      }
      .word {
        color: #2f3742;
        margin-left: 14px;
        padding-right: 48px;
        font-size: 16px;
        font-weight: 600;
        line-height: 22px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        p {
          color: #95a1af;
          font-size: 12px;
          line-height: 36px;
        }
      }
    }
    .free-class-list {
      ul {
        // padding:0 10px;
        li {
          display: flex;
          background-color: #fff;
          padding: 0 10px;
          padding-bottom: 17px;
          .smalltag {
            color: #ff3271;
            border: 1px solid #ffa8c3;
            background-color: #ffeff4;
            width: 26px;
            padding: 2px 5px;
            height: 14px;
            font-size: 12px;
            text-align: center;
            line-height: 14px;
            border-radius: 5px;
            margin-right: 6px;
            margin-top: 7px;
          }
          .nulltag {
            @extend .smalltag;
            background-color: transparent;
            border-color: transparent;
          }
          .free-right {
            flex: 1;
          }
          .free-class-name {
            font-size: 16px;
            color: #2f3742;
            line-height: 28px;
            width: 260px;
          }
          .free-count {
            font-size: 12px;
            color: #95a1af;
            line-height: 23px;
          }
        }
      }
    }
  }
  .more {
    text-align: center;
    color: #63abff;
    line-height: 40px;
  }
}
.bottom {
  height: 50px;
  background-color: #f6f7f9;
  border-radius: 10px;
}
</style>